// 创建模块
var app = angular.module('starterApp', 
    ['ngSanitize', 'ngAnimate', 'ngRoute', 'starter.tag.controllers',
      'starter.controllers', 'starter.services', 
      'starter.filters', 'starter.directives']);

app.config(['$routeProvider', '$locationProvider', function ($routeProvider, $locationProvider) {  
    $routeProvider
        .when('/admin',{
            templateUrl: "admin.html"
        })
        .when('/admin-upload/:sourceType?', {
            templateUrl: '/templates/admin_upload.html'
        })
        .when('/admin-upload-logo/:sourceType?', {
            templateUrl: '/templates/admin_upload_logo.html',            
        }).otherwise({redirectTo: '/admin-upload'});

}]);


app.run(['$rootScope', '$location', '$log', function ($rootScope, $location, $log) {  
    var locationChangeStartOff = $rootScope.$on('$locationChangeStart', locationChangeStart);  
    var locationChangeSuccessOff = $rootScope.$on('$locationChangeSuccess', locationChangeSuccess);  
  
    var routeChangeStartOff = $rootScope.$on('$routeChangeStart', routeChangeStart);  
    var routeChangeSuccessOff = $rootScope.$on('$routeChangeSuccess', routeChangeSuccess);  
  
    function locationChangeStart(event) {  
        //$log.log('locationChangeStart');  
        //$log.log(arguments);  

    }  
  
    function locationChangeSuccess(event) {  
        //$log.log('locationChangeSuccess');  
        //$log.log(arguments);  
    }  
  
    function routeChangeStart(event) {  
        //$log.log('routeChangeStart');  
        //$log.log(arguments);  
        //App.blockUI({target: $(".show-type-list"), iconOnly: true});
        //$rootScope.ready = true;
        //console.log($rootScope.ready)
    }  
  
    function routeChangeSuccess(event) {  
        //$log.log('routeChangeSuccess');  
        //$log.log(arguments);  
        //App.unblockUI($(".show-type-list"));
        
    }  
}]);

angular.module('starter.tag.controllers', [])
.controller('starterCtl', ['$rootScope', '$scope', '$location', '$routeParams', function($rootScope, $scope, $location, $routeParams){
    $scope.source_admin_type = $routeParams.sourceType || 1;

    var initAdmin = function(){
        $("li[role=adminManage]").removeClass("active");
        $("#tab_admin").parent().addClass("active");
    };

    var initAdminUpload = function(){
        $("li[role=adminManage]").removeClass("active");
        $("#tab_admin_upload").parent().addClass("active");

    };

    var initAdminUploadLogo = function(){
        $("li[role=adminManage]").removeClass("active");
        $("#tab_admin_upload_logo").parent().addClass("active");

        var opt = {
            upload_type: "Logo",
            flash_url: baseUrl + "javascripts/swfupload/swfupload.swf",
            flash9_url: baseUrl + "javascripts/swfupload/swfupload_fp9.swf",
            upload_url: baseUrl + "admin/upload-logo",
            post_params:{
                auth_user_id: "userId"
            },

            button_text: '<span class="button">Select image</span>',
            button_text_style: '.button{ font-family:Arial;font-size:14px;text-align:center;height:36px; line-height:36px; font-weight:bold; color:#288DB8; display:inline-block; *display:inline; *zoom:1; box-shadow:1px 1px 2px rgba(0,0,0,0.15); }',
            button_image_url: baseUrl + "images/flash-button.png",
            button_disabled: false,  //true 触发mouseclick 事件
            button_action:  SWFUpload.BUTTON_ACTION.SELECT_FILE,   //SELECT_FILE 单选  SELECT_FILES 多选
            file_size_limit: "2 MB",     //kb
            file_upload_limit: 0, //    0 - unlimited
            file_types: "*.jpg;*.png;.jpeg",
            file_types_description: "Files of type",
        };
        //速度太快没加载出来flash，不知道为什么？
        setTimeout(function(){Flash.init(opt)}, 1000);
    };

    $scope.$on('$viewContentLoaded', function() {  
        //console.log($location);
        switch($location.$$path){
            case "/admin":
                initAdmin();
                break;
            case "/admin-upload":
                initAdminUpload();
                break;
            case "/admin-upload-logo":
                initAdminUploadLogo();
                break;
            default:break;
        }

        //$rootScope.ready = false;
        //console.log($rootScope.ready)
    });  
}])
